package com.lkm.app_ui.m.Imple.sz_dataup;

import android.content.Context;
import android.os.SystemClock;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.m.dataUpExt.DataUpExtModule;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.m.dataUp.SZUpdateStatus;
import com.lkm.market2_0.m_imple.newDataUp.NewDataUpParse;
import com.lkm.market2_0.m_imple.newDataUp.NewSZApi;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;



public class SZDataUpModuleImpl implements DataUpExtModule {


    private SZOrderUpTask szOrderUpTask = null;

    public SZDataUpModuleImpl() {
        if(!StringHelp.isEmpty(NewSZApi.getHost()))
            start();
    }


    @Override
    public synchronized void submitWeightInfo(SteelyardInfo steelyardInfo, OrderEntity orderEntity) {
        if(szOrderUpTask != null)
            szOrderUpTask.addUpOrder(steelyardInfo, orderEntity);
    }


    private Boolean hasReg = null;

    private long lasetStatustime = 0;
    //10秒一次心跳。那我就 等待2.5分钟一次吧。 2.5*60
    @Override
    public void upState(SteelyardInfo steelyardInfo, boolean isExceptions) {

        if(!isStart)return;

        long curTime = SystemClock.uptimeMillis();
        if(curTime - lasetStatustime < (2.7 * 60 * 1000)){
            return;
        }

        lasetStatustime = curTime;
        AppConst.getTaskExecutor().execute(new Runnable() {
            @Override
            public void run() {
                SZUpdateStatus szWeightInfo = NewDataUpParse.parseSZUpdateStatus(steelyardInfo, isExceptions);
                if (szWeightInfo != null) {
                    String s = NewSZApi.updateStatus(steelyardInfo.getSteelyardMac(),szWeightInfo, null, true);

                    if(!StringHelp.isEmpty(s)) {
                        boolean isSave = false;
                        if(hasReg == null){
                            isSave = true;
                            hasReg = false;
                        }
                        if (NewSZApi.checkIsSuccess(s)) {
                            if(hasReg == false){

                                isSave = true;
                            }
                            hasReg = true;
                        } else {
                            if(hasReg == true){

                                isSave = true;
                            }
                            hasReg = false;
                        }

                        if(isSave) {
                            MyApplication.getInstance().getSharedPreferences("settingapp", Context.MODE_PRIVATE).edit().putBoolean("device_hasReg", hasReg).apply();
                        }


                    }
                }
            }
        });


    }

    private volatile boolean isStart = false;
    @Override
    public synchronized void stop() {
        isStart = false;
        if (szOrderUpTask == null) return;
        szOrderUpTask.cancel();
        szOrderUpTask = null;
    }

    @Override
    public synchronized void start() {
        if(isStart)return;
        isStart = true;
        szOrderUpTask = new SZOrderUpTask();
        szOrderUpTask.action();
        szOrderUpTask.execTask();
    }

    @Override
    public void setHost(String host) {
        if (StringHelp.isEmpty(host)) {
            stop();
        } else {
            start();
        }
    }
}
